/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package br.com.murilojava.compilador.view;

import java.io.BufferedReader;
import java.io.StringReader;
import java.util.List;
import java.util.Vector;

import javax.swing.JOptionPane;

import br.com.murilojava.compilador.model.Token;
import br.com.murilojava.compilador.scanner.Scanner;

/**
 * 
 * @author Murilo
 */
public class CompiladorFrame extends javax.swing.JFrame {

	/**
	 * Creates new form CompiladorFrame
	 */
	public CompiladorFrame() {
		initComponents();
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed"
	// desc="Generated Code">//GEN-BEGIN:initComponents
	private void initComponents() {

		tabPanel = new javax.swing.JTabbedPane();
		codePanel = new javax.swing.JPanel();
		jScrollPane1 = new javax.swing.JScrollPane();
		codigoTextArea = new javax.swing.JTextArea();
		buttonBar = new javax.swing.JPanel();
		analisarButton = new javax.swing.JButton();
		compilarButton = new javax.swing.JButton();
		erroPanel = new javax.swing.JPanel();
		jScrollPane2 = new javax.swing.JScrollPane();
		listaErroList = new javax.swing.JList();
		saidaPanel = new javax.swing.JPanel();
		jScrollPane3 = new javax.swing.JScrollPane();
		listaSaidaList = new javax.swing.JList();

		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
		setTitle("Compiler 0.1");

		codigoTextArea.setColumns(20);
		codigoTextArea.setRows(5);
		codigoTextArea.setBorder(new NumeredBorder());
		jScrollPane1.setViewportView(codigoTextArea);

		analisarButton.setText("Analisar");
		analisarButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				analisarButtonActionPerformed(evt);
			}
		});
		buttonBar.add(analisarButton);

		compilarButton.setText("Compilar");
		compilarButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				compilarButtonActionPerformed(evt);
			}
		});
		buttonBar.add(compilarButton);

		javax.swing.GroupLayout codePanelLayout = new javax.swing.GroupLayout(
				codePanel);
		codePanel.setLayout(codePanelLayout);
		codePanelLayout
				.setHorizontalGroup(codePanelLayout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								codePanelLayout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												codePanelLayout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addComponent(
																jScrollPane1)
														.addComponent(
																buttonBar,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																Short.MAX_VALUE))
										.addContainerGap()));
		codePanelLayout
				.setVerticalGroup(codePanelLayout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								codePanelLayout
										.createSequentialGroup()
										.addContainerGap()
										.addComponent(
												jScrollPane1,
												javax.swing.GroupLayout.DEFAULT_SIZE,
												387, Short.MAX_VALUE)
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
										.addComponent(
												buttonBar,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												javax.swing.GroupLayout.DEFAULT_SIZE,
												javax.swing.GroupLayout.PREFERRED_SIZE)));

		tabPanel.addTab("Código", codePanel);

		jScrollPane2.setViewportView(listaErroList);

		javax.swing.GroupLayout erroPanelLayout = new javax.swing.GroupLayout(
				erroPanel);
		erroPanel.setLayout(erroPanelLayout);
		erroPanelLayout.setHorizontalGroup(erroPanelLayout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addComponent(
				jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 395,
				Short.MAX_VALUE));
		erroPanelLayout.setVerticalGroup(erroPanelLayout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addComponent(
				jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 272,
				Short.MAX_VALUE));

		tabPanel.addTab("Erros", erroPanel);

		jScrollPane3.setViewportView(listaSaidaList);

		javax.swing.GroupLayout saidaPanelLayout = new javax.swing.GroupLayout(
				saidaPanel);
		saidaPanel.setLayout(saidaPanelLayout);
		saidaPanelLayout.setHorizontalGroup(saidaPanelLayout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addComponent(jScrollPane3,
						javax.swing.GroupLayout.DEFAULT_SIZE, 395,
						Short.MAX_VALUE));
		saidaPanelLayout.setVerticalGroup(saidaPanelLayout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addComponent(
				jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 272,
				Short.MAX_VALUE));

		tabPanel.addTab("Saída", saidaPanel);

		getContentPane().add(tabPanel, java.awt.BorderLayout.CENTER);
		tabPanel.getAccessibleContext().setAccessibleName("codeTab");

		pack();
	}// </editor-fold>//GEN-END:initComponents

	private void analisarButtonActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_analisarButtonActionPerformed
		analisar();
	}// GEN-LAST:event_analisarButtonActionPerformed

	private void compilarButtonActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_compilarButtonActionPerformed
		compilar();
	}// GEN-LAST:event_compilarButtonActionPerformed

	// Variables declaration - do not modify//GEN-BEGIN:variables
	private javax.swing.JButton analisarButton;
	private javax.swing.JPanel buttonBar;
	private javax.swing.JPanel codePanel;
	private javax.swing.JTextArea codigoTextArea;
	private javax.swing.JButton compilarButton;
	private javax.swing.JPanel erroPanel;
	private javax.swing.JScrollPane jScrollPane1;
	private javax.swing.JScrollPane jScrollPane2;
	private javax.swing.JScrollPane jScrollPane3;
	private javax.swing.JList listaErroList;
	private javax.swing.JList listaSaidaList;
	private javax.swing.JPanel saidaPanel;
	private javax.swing.JTabbedPane tabPanel;

	// End of variables declaration//GEN-END:variables

	private Scanner analisador = new Scanner();

	private void analisar() {
		analisador.setInput(new BufferedReader(new StringReader(codigoTextArea
				.getText())));

		try {
			analisador.analisar();

		} catch (Exception e) {
			JOptionPane.showMessageDialog(this, "Erro ao realizar a análise "
					+ e.getMessage());
			e.printStackTrace();
		}
		Vector<String> erros = new Vector<String>();

		Vector<String> saida = new Vector<String>();

		List<Token> tokens = analisador.getBuffer();
		for (Token token : tokens) {
			if (token.getErro())
				erros.add("VALOR: " + token.getLexema() + " LINHA: "
						+ token.getLinha() + " TIPO: " + token.getTipo()
						+ "? ERRO: " + token.getErroMsg());
			else
				saida.add(token.getTipo() + " -->  " + token.getLexema());
		}

		listaErroList.setListData(erros);
		listaSaidaList.setListData(saida);

		if (!erros.isEmpty()) {
			tabPanel.setSelectedIndex(1);
		} else if (!saida.isEmpty()) {
			tabPanel.setSelectedIndex(2);
		}
	}

	private void compilar() {

	}
}
